package us.bighelp2.marc.dataservices;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.annotations.common.util.StringHelper;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import us.bighelp2.marc.common.AppDBConnection;
import us.bighelp2.marc.common.Queries;

public class ApplicationDataService extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(ApplicationDataService.class);
       
    public ApplicationDataService() 
    {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
		doPost(request,response);
	}
    
	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		if(StringHelper.isNotEmpty(request.getParameter("requestType")))
		{
			response.setContentType("application/json");
			PrintWriter pw = response.getWriter();
			String requestType = request.getParameter("requestType");
			AppDBConnection dbconn = new AppDBConnection("marc");
			
			if("gatherApplicationSettings".equals(requestType))
			{
				JSONObject output = new JSONObject();
				try
				{
					Connection conn = dbconn.getConnection();
					
					//Gather Settings
					PreparedStatement ps = conn.prepareStatement(Queries.SQL_GATHER_APPLICATION_SETTINGS);
					ResultSet rs = ps.executeQuery();
					
					try
					{
						//Container for settings
						JSONObject appSettings = new JSONObject();
						JSONObject privateSettings = new JSONObject();
						
						//Loop through settings from DB
						while(rs.next())
						{
							if(rs.getInt("userviewable") == 1)
								appSettings.put(rs.getString("key"), rs.getString("value"));
							else
								privateSettings.put(rs.getString("key"), rs.getString("value"));
						}
						
						//Return settings to front-end
						output.put("AppSettings", appSettings);
						
					}
					finally
					{
						rs.close();
						ps.close();
					}
					
					//Gather Modules
					
					PreparedStatement psMods = conn.prepareStatement(Queries.SQL_GATHER_AVAILABLE_MODULES);
					//String username = request.getParameter("username");
					//psMods.setString(1,username);
					ResultSet rsMods = psMods.executeQuery();
					
					try
					{
						//Container for settings
						JSONArray availMods = new JSONArray();
						
						//Loop through modules from DB
						while(rsMods.next())
						{
							JSONObject curMod = new JSONObject();
							curMod.put("Name", rsMods.getString("moduleName"));
							curMod.put("URL", rsMods.getString("moduleUrl"));
							curMod.put("Body", rsMods.getString("moduleBody"));
							
							availMods.add(curMod);
						}
						
						//Return settings to front-end
						output.put("AvailableModules", availMods);
						
					}
					finally
					{
						rsMods.close();
						psMods.close();
					}
				}
				catch(SQLException se)
				{
					se.printStackTrace();
				}
				finally
				{
					dbconn.closeConnection();
					pw.println(output.toString());
				}
			}
		}
	}
}
